#define _CRT_SECURE_NO_WARNINGS 1
#include "Heap.h"

void HPInit(HP* php)
{
	assert(php);

	php->a = NULL;
	php->size = 0;
	php->capacity = 0;
}

void AdjustDown(HPDatatype* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] < a[parent])
		{
			HPDatatype tmp = a[child];
			a[child] = a[parent];
			a[parent] = tmp;

			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}


void HPPush(HP* php, HPDatatype x)
{
	assert(php);

	if (php->size == php->capacity)
	{
		int newcapacity = (php->capacity == 0) ? 4 : 2 * php->capacity;
		HPDatatype* tmp = (HPDatatype*)realloc(php->a,sizeof(HPDatatype) * newcapacity);
		if (tmp== NULL)
		{
			perror("realloc fail");
			return;
		}

		php->capacity = newcapacity;
		php->a = tmp;
	}

	php->a[php->size++] = x;

	AdjustDown(php->a, php->size - 1);
}